$(function(){
	/*** 初始化专业树 */
	$('#subject_tree').tree({
	    url : 'subject/subjects.shtml',
	    /*onLoadSuccess:function(node,data){
	         var t = $(this);
	  		 if(data){
			     $(data).each(function(index,d){
					if(this.state == 'closed'){
					    t.tree('expandAll');
					}
			     });
			}
	    },*/
	    onClick : function (node) {
	    	var params = {};
	    	var _nodeType = node.nodeType;
	    	var _param = _nodeType + 'Id';
	    	params[_param] = node.id;
	    	//刷新教案列表
	    	$('#dg').datagrid('reload', params);
	    }
	});
	
	/** 附件列表 */
	$('#dg').datagrid({    
	    //title:"任务反馈列表",
		border:0,
		nowrap:true,
		striped:true,
		fit: true,//自动大小
		url:'resource/queryBySubjectId.shtml',
//		queryParams : {bizId : $('#biz_id').val()},
		frozenColumns:[[    
	        {field : 'att_id', title : 'att_id', hidden:true},    
	        {field : 'file_name', title : '文件名称', width:300,
	        	formatter : function (value, row, index) {
	        		//return '<a href="' + contextPath + '/attachment/down?attId=' + row.att_id + '" target="_blank">' + value + '</a>';
//	        		return '<a href="/proms/attachment/down?attId=' + row.att_id + '" target="_blank">' + value + '</a>';
	        		//<a target="_blank" href="/proms/attachment/down?attId=71">Chrysanthemum.jpg</a>
	        		return dealClickAtta(row.att_id, value);
	        	}
	        }
	    ]],
	    columns : [[
			{field : 'title', title : '标题', width : 150},
			{field : 'name', title : '学科名称', width : 200},
			{field : 'people', title : '创建人', width : 200},
			{field : 'remark', title : '备注', width : 200},
			{field : 'createTime', title : '创建时间', width:150}
        ]],
		singleSelect:true,
		rownumbers:true,
		toolbar:'#tb',
		pagination:true,
		pageSize:20,
		onClickRow:function(rowIndex, rowData){}
	}).datagrid('getPager').pagination({  
        pageSize: 20,//每页显示的记录条数，默认为10  
        pageList: [20,50,100],//可以设置每页记录条数的列表  
        beforePageText: '第',//页数文本框前显示的汉字  
        afterPageText: '页    共 {pages} 页',  
        displayMsg: '当前显示 {from} - {to} 条记录   共 {total} 条记录'
    });
});

/*** 重载页面 */
function reSearch() {
	$('#title').val('');
	doSearch();
}
/*** 搜索 */
function doSearch() {
	$('#dg').datagrid('reload', {
		title : $('#title').val()
	});
}
/**
 * 将long型转成date型
 * @param value
 * @param row
 * @returns
 */
function formatterDate(value, row) {
	return formatDate(value, 'yyyy-MM-dd hh:mm:ss');
}

function dealClickAtta(attId, fileName) {
	if(isVideo(fileName)) {
		return '<a href="javascript:void(0);" onclick="playVideo(\'' + attId + '\')">' + fileName + '</a>';
	} else {
		return '<a href="' + contextPath + '/attachment/down?attId=' + attId + '" target="_blank">' + fileName + '</a>';
	}
	
}

function isVideo(fileName) {
	var videoStr = "mp4";
	var suffix = getFileSuffix(fileName);
	if(videoStr.indexOf(suffix)<0){
		suffix = "epub";
	} 
	if(videoStr.indexOf(suffix)<0){
		return false;
	}
	return true;
}

function getFileSuffix(fileName){ 
	if(typeof fileName === 'undefined'){
		return '';
	}else{
		var len = fileName.lastIndexOf('.'); 
		return fileName.substr(len+1,fileName.length);
	}
} 

function playVideo(attId) {
	showEasyUiWindow('contentFrame', 'video/listByAttId.shtml?attId=' + attId, 'win', '视频播放', 640, 380);
}